package zack.gateway.core.session.defaults;

import zack.gateway.core.bind.IGenericReference;
import zack.gateway.core.executor.Executor;
import zack.gateway.core.mapping.HttpStatement;
import zack.gateway.core.session.Configuration;
import zack.gateway.core.session.GatewaySession;

import java.util.Map;

/**默认网关会话实现类,负责与rpc进行连接
 * @author A1793
 */
public class DefaultGatewaySession implements GatewaySession {
    private Configuration configuration;
    private String uri;
    private Executor executor;

    public DefaultGatewaySession(Configuration configuration,String uri,Executor executor) {
        this.configuration = configuration;
        this.executor = executor;
        this.uri = uri;
    }

    /**
     * 获取数据源连接
     * @param methodName
     * @param parameters
     * @return
     */
    @Override
    public Object get(String methodName, Map<String,Object> parameters) {

        HttpStatement httpStatement = configuration.getHttpStatement(uri);
        try {
            return executor.exec(httpStatement, parameters);
        } catch (Exception e) {
            throw new RuntimeException("Error exec get. Cause: " + e);
        }
    }

    @Override
    public Object post(String methodName, Map<String, Object> parameter) {
        return get(methodName, parameter);
    }

    @Override
    public IGenericReference getMapper() {
        return configuration.getMapper(uri,this);
    }

    @Override
    public Configuration getConfiguration() {
        return configuration;
    }
}
